
package guoan.web.webCustomer;

import guoan.core.common.WebConfig;
import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.Train;
import guoan.service.TrainService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/webCustomer/train")
public class TrainController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private TrainService trainService;

    /** 跳转到考试主页 */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(){
        return "/webCustomer/train/index";
    }

    /** 跳转到添加预约考试 */
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String toAdd(Model model){
        // 写入预约考试默认的
        model.addAttribute("examRanges", WebConfig.EXAM_OPENHOURS_RANGE);
        return "/webCustomer/train/add";
    }

    /** 保存预约考试 */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> add(Train train){
        Preconditions.checkNotNull(train, "请填写预约考试");
        return trainService.add(train);
    }

    /** 保存预约考试 */
    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String toUpdate(Long id, Model mpdel){
        Preconditions.checkNotNull(id, "参数错误");
        mpdel.addAttribute("train", trainService.findOne(id));
        return "/webCustomer/train/update";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> update(Train train){
        Preconditions.checkNotNull(train, "请填写预约考试");
        return trainService.update(train);
    }

    @RequestMapping(value = "data", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<Train> data(DataGridModel dataGridModel){
        return trainService.page(dataGridModel);
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> del(Long id){
        return trainService.del(id);
    }
}
